﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class DirectsellByID : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["cjoid"] != null && Request["cjoid"].ToString().Trim() != "")
                    bindData(Request["cjoid"].ToString().Trim());
                else
                    DataLib.Common.MessageBox.ShowAndRedirect(Page, "非法进入", "Directsell.aspx");
            }
        }

        private void bindData(string cjoid)
        {
            DataLib.BLL.Cjo cjobll = new DataLib.BLL.Cjo();
            DataLib.Model.Cjo cjomodel = new DataLib.Model.Cjo();
            cjomodel = cjobll.GetModel(int.Parse(cjoid));

            tdLoadnum.ToolTip = cjomodel.loadnumid.ToString();
            tdLoadnum.Text = cjomodel.loadnum; //dir.loadnumno;

            lblCjoNo.ToolTip = cjomodel.id.ToString();
            lblCjoNo.Text = cjomodel.cjono;

            tdCumCode.Text = cjomodel.customercode;
            tdCumCode.ToolTip = cjomodel.customerid.ToString();
            tdCumName.InnerText = cjomodel.customername;
            tdsalesman.InnerText = cjomodel.sealman_show;

            DataTable dt = new DataLib.BLL.Directsell().GetList("cjoid=" + cjoid).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                string dirid = dt.Rows[0]["id"].ToString();
                DataLib.Model.Directsell dir = new DataLib.BLL.Directsell().GetModel(Convert.ToInt32(dirid));
                txtDescirbe.Value = dir.Describe;
                txtGoodsWeight.Value = dir.goodsWeight.ToString();
                hiddirid.Value = dirid;
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataLib.Model.Directsell dir = new DataLib.Model.Directsell();
            dir.goodsWeight = Convert.ToDecimal(txtGoodsWeight.Value);
            dir.Describe = txtDescirbe.Value.ToString();
            dir.customerid = Convert.ToInt32(tdCumCode.ToolTip);
            dir.customerCode = tdCumCode.Text;
            dir.customerName = tdCumName.InnerHtml;
            dir.loadnumid = Convert.ToInt32(tdLoadnum.ToolTip);
            dir.loadnumno = tdLoadnum.Text;
            dir.cjoid = Convert.ToInt32(lblCjoNo.ToolTip);
            dir.cjono = lblCjoNo.Text;
            DataLib.Model.LTR_CJO ltrmodel = new DataLib.Model.LTR_CJO();
            DataLib.BLL.LTR_CJO ltrbll = new DataLib.BLL.LTR_CJO();
            ltrmodel = ltrbll.GetModelByCjoID(dir.cjoid);
            ltrmodel.nwt = dir.goodsWeight;
           
            if (hiddirid.Value != "0" )
            {
                dir.id = int.Parse(hiddirid.Value);
                 
                new DataLib.BLL.Directsell().Update(dir);
                ltrmodel.directsellid = dir.id;
                ltrbll.Update(ltrmodel);               
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "修改成功", "Directsell.aspx");
            }
            else
            {
                ltrmodel.directsellid = new DataLib.BLL.Directsell().Add(dir);
                ltrbll.Update(ltrmodel);
                DataLib.Common.MessageBox.ShowAndRedirect(Page, "添加成功", "Directsell.aspx");
            }
            
        }
    }
}